/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.com.remiseria.dao;

import ar.com.remiseria.entities.Cliente;
import ar.com.remiseria.utils.HibernateUtils;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author mcvalls
 */
public class ClienteDAO extends GenericDAO{
    
    
    Session session = HibernateUtils.getSessionFactory().getCurrentSession();
    
    
    public Cliente getClienteByTelefono(String telefono) throws HibernateException{
        
        Transaction tx = session.beginTransaction();
        
        Criteria criteria = session.createCriteria(Cliente.class);
        criteria.add(Restrictions.eq("telefono", telefono));
        
        Cliente cliente = (Cliente) criteria.uniqueResult();
        
        return cliente;
    }

    public List<Cliente> getAllByFiltro(int start, String palabraClave) throws HibernateException{
        
        Transaction tx = session.beginTransaction();
        
        Query query = session.createQuery("from Cliente where"
                + " nombre = "+palabraClave+" or"
                + " apellido = "+palabraClave+" or"
                + " telefono = "+palabraClave+" or"
                + " documento.numero = "+palabraClave);
                
        
        List<Cliente> losClientes = (List<Cliente>) query.list();
        
        tx.commit();
        
        return losClientes;
    }
    
}
